<?php

/**
 * 公共操作
 * @author wyb
 */

namespace Seller\Controller;

class PublicController extends CommController {

    private $_model = null;

    public function _initialize() {
        parent::_initialize();
        $this->_model = M('customer');
    }

    /**
     * 商家登录
     */
    public function login() {
        if (isset($_SESSION[$this->_soller_login_key]) && $_SESSION[$this->_soller_login_key]) {
            header('Location:' . U('Index/index'));
            exit;
        }
        $info['customerCount'] = M('customer')->count();
        $info['supplyCount'] = M('goods_supply')->count() + 1000;
        $this->assign('info', $info);
        $this->display();
    }

    /**
     * 商家登录检测
     */
    public function loginChecked() {
        isset($this->__post['user']) && $this->__post['user'] ? '' : $this->error('商家登录用户名不能为空');
        isset($this->__post['pass']) && $this->__post['pass'] ? '' : $this->error('商家登录密码不能为空');
        $customer = $this->_model->where(array('username' => $this->__post['user'], 'status' => 1))->find();
        //用户密码认证
        if ($customer && $customer['password'] == password($this->__post['pass'])) {
            if ($customer['status'] == 1) {
                $customer['grade_data'] = '';
                if ($customer['grade_id'] > 0) {
                    $grade_data = M('grade')->find($customer['grade_id']);
                    if ($grade_data) {
                        $grade_data['authority'] = $grade_data['authority'] ? unserialize($grade_data['authority']) : '';
                        $customer['grade_data'] = $grade_data;
                    }
                }
                $customer_save_data = array(
                    'now_login_time' => time(),
                    'now_login_ip' => get_client_ip(),
                    'last_login_time' => $customer['now_login_time'],
                    'last_login_ip' => $customer['now_login_ip'],
                    'login_num' => $customer['login_num'] + 1
                );
                $customer = array_merge($customer, $customer_save_data);
                $_SESSION[$this->_soller_login_key] = $customer;
                M('customer')->save($customer);
                $this->success('商家【' . $customer['shop_name'] . '】登录成功', U('Index/index'));
            } else {
                $this->error('商家还未审核通过，暂不能登录');
            }
        } else {
            $this->error('商家登录失败');
        }
    }

    /**
     * 商家退出
     */
    public function outLogin() {
        $_SESSION[$this->_soller_login_key] = '';
        session_unset($this->_soller_login_key);
        session_destroy();
        header('Location:' . U('Public/login'));
    }

    /**
     * 商家注册
     */
    public function register() {
        if (IS_POST) {

            $data = $this->__post;
            if (empty($data['company_id'])) {
                $this->error('请绑定企业身份！');
                exit;
            }
            if (empty($data['contact_tel'])) {
                $this->error('电话号码不能为空！');
                exit;
            }
            if ($data['code'] != S($this->_customer_code_key)) {
                $this->error('验证码错误');
                exit;
            }
            if (M('customer')->where(array('contact_tel' => $this->__post['contact_tel']))->find()) {
                $this->error('该手机号码已经注册！');
                exit;
            } else {

                $data['password'] = password($data['password']);
                $data['create_time'] = time(0);

                if (M('customer')->add($data)) {
                    $this->success('注册成功！', U('login'));
                    exit;
                } else {
                    $this->error('注册失败！');
                    exit;
                }
            }
        }
        $this->assign('company', M('company')->select());
        $this->display();
    }

    /**
     * 商家手机号认证
     */
    public function mobileCheck() {
        isset($this->__get['mobile']) && $this->__get['mobile'] ? $mobile = $this->__get['mobile'] : $this->ajaxReturn(array('status' => 0, 'msg' => '手机号码不能为空'));
        if (M('customer')->where(array('contact_tel' => $this->__get['mobile']))->find()) {
            $this->ajaxReturn(array('status' => 0, 'msg' => '手机号已经注册'));
            exit;
        }
        $rand_data = rand(100000, 999999);
        S($this->_customer_code_key, $rand_data, array('expire' => 3000));
        \Common\Api\SmsApi::init()->sendSms('您的验证码：' . $rand_data . '，切记打死都不能给别人说哦。', $mobile);
        $this->ajaxReturn(array('status' => 1, 'msg' => '发送成功'));
    }

}
